<template>
  <div class="cargo-detail">
    <el-page-header @back="goBack" title="返回货物管理">
      <template #content>
        <span class="page-title">货物详情</span>
      </template>
    </el-page-header>
    
    <el-card class="detail-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>基本信息</span>
        </div>
      </template>
      
      <el-descriptions :column="2" border>
        <el-descriptions-item label="货物ID">{{ cargo.cargoId }}</el-descriptions-item>
        <el-descriptions-item label="货物名称">{{ cargo.billNo }}</el-descriptions-item>
        <el-descriptions-item label="货物类型">{{ cargo.cargoType }}</el-descriptions-item>
        <el-descriptions-item label="所属客户">{{ getClientName(cargo.ownerId) }}</el-descriptions-item>
        <el-descriptions-item label="重量(吨)">{{ cargo.weight }}</el-descriptions-item>
        <el-descriptions-item label="体积(m³)">{{ cargo.containerSize }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(cargo.status)">
            {{ getStatusText(cargo.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDate(cargo.createdAt) }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'CargoDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const cargo = ref({})
    const loading = ref(false)
    const clients = ref([])
    
    const fetchCargoDetail = async () => {
      const cargoId = route.params.id
      if (!cargoId) return
      
      loading.value = true
      try {
        const response = await axios.get(`/cargos/${cargoId}`)
        cargo.value = response.data.data
        // 获取客户信息
        await fetchClients()
      } catch (error) {
        console.error('获取货物详情失败:', error)
        ElMessage.error('获取货物详情失败')
      } finally {
        loading.value = false
      }
    }

    const fetchClients = async () => {
      try {
        const response = await axios.get('/clients')
        clients.value = response.data.data
      } catch (error) {
        console.error('获取客户列表失败:', error)
      }
    }
    
    const getClientName = (clientId) => {
      if (!clientId || clients.value.length === 0) return '未知'
      const client = clients.value.find(c => c.clientId === clientId)
      return client ? client.companyName : '未知'
    }
    
    const getStatusType = (status) => {
      if (!status) return ''
      switch(status.toLowerCase()) {
        case 'stored':
          return 'success'
        case 'in_transit':
          return 'warning'
        case 'delivered':
          return 'info'
        default:
          return ''
      }
    }
    
    const getStatusText = (status) => {
      if (!status) return '未知'
      switch(status.toLowerCase()) {
        case 'stored':
          return '已入库'
        case 'in_transit':
          return '运输中'
        case 'delivered':
          return '已交付'
        default:
          return '未知'
      }
    }
    
    const goBack = () => {
      router.back()
    }
    
    const formatDate = (dateString) => {
      if (!dateString) return '无'
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN', { 
        year: 'numeric', 
        month: '2-digit', 
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit' 
      })
    }
    
    onMounted(() => {
      fetchCargoDetail()
    })
    
    return {
      cargo,
      loading,
      goBack,
      formatDate,
      getStatusType,
      getStatusText,
      getClientName
    }
  }
}
</script>

<style scoped>
.cargo-detail {
  padding: 20px;
}

.detail-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  font-weight: bold;
  font-size: 18px;
}
</style>